<?php
namespace App\Http\Controllers\Admin;

use App\Service\Admin\TmpDocService;
use App\Utilitys\Upload;
use Illuminate\Http\Request;
use ZipArchive as Zip;

class TmpDocController extends BaseController
{
    //模板接口文档

    public function __construct(TmpDocService $service)
    {
        $this->db = $service;
    }

    //列表
    public function getIndex(Request $request)
    {
//        $token = $request->post('token', '');
//        $uid = $this->db->getUidByToken($token);
//        if (!$uid) {
//            return $this->toJson(-2, '没有登录');
//        }
        $item_id = $request->post('item_id', 0); //项目ID
        $client_type = $request->post('client_type', 0); //用户端类型
        $res = $this->db->getIndex($item_id, $client_type);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //详情
    public function getOne(Request $request)
    {
        $doc_id = $request->post('doc_id', 0);
        $res = $this->db->getOne($doc_id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //添加
    public function add(Request $request)
    {
        $pas['item_id'] = $request->post('item_id', 0); //项目ID
        $pas['client_type'] = $request->post('client_type', 0); //用户端类型：1前台，2后台
        $pas['title'] = $request->post('title', ''); //接口名称
        $pas['url'] = $request->post('url', ''); //接口地址
        $pas['req_json'] = $request->post('req_json', ''); //请求参数
        if (!$pas['title'] || !$pas['url']) {
            return $this->toJson(-2, '接口名或地址必须的');
        }
        if (!in_array($pas['client_type'], [1, 2])) {
            return $this->toJson(-2, '用户端类型参数错误');
        }
        $res = $this->db->add($pas);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //修改
    public function modify(Request $request)
    {
        $doc_id = $request->post('id', 0); //接口ID
        $pas['title'] = $request->post('title', ''); //接口名称
        $pas['url'] = $request->post('url', ''); //接口地址
        $pas['req_json'] = $request->post('req_json', ''); //请求参数
        if (!$pas['title'] || !$pas['url']) {
            return $this->toJson(-2, '接口名或地址必须的');
        }
        $res = $this->db->modify($doc_id, $pas);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //删除
    public function del(Request $request)
    {
        $doc_id = $request->post('doc_id', 0); //接口ID
        $res = $this->db->del($doc_id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }
}